﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;


namespace Vertex3dDemo2
{


    struct Vertex3d
    {
        private double _x;
        private double _y;
        private double _z;

        public double X
        {
            get { return _x; }
            set { _x = value; }
        }
        public double Y
        {
            get { return _y; }
            set { _y = value; }
        }

        public double Z
        {
            get { return _z; }
            set { _z = value; }
        }
        public Vertex3d(double x, double y, double z)
        {
            this._x = x;
            this._y = y;
            this._z = z;
        }

        public override string ToString()
        {
            return string.Format("({0}, {1}, {2})", X, Y, Z);
        }


        /* IFormattable.ToString */
        public string ToString(string format, IFormatProvider formatProvider)
        {
            

            //”G” is .Net’s standard for general formatting--all
            //types should support it
            if (format == null) format = "G";
            // is the user providing their own format provider?
            if (formatProvider != null)
            {
                ICustomFormatter formatter =
                    formatProvider.GetFormat(this.GetType())
                    as ICustomFormatter;

                if (formatter != null)
                {
                    return formatter.Format(format, this, formatProvider);
                }
            }
            //formatting is up to us, so let’s do it
            if (format == "G")
            {
                return string.Format("({0}, {1}, {2})", X, Y, Z);
            }

            StringBuilder sb = new StringBuilder();
            int sourceIndex = 0;

            while (sourceIndex < format.Length)
            {
                switch (format[sourceIndex])
                {
                    case 'X':
                    sb.Append(X.ToString());
                    break;
                    case 'Y':
                    sb.Append(Y.ToString());
                    break;
                    case 'Z':
                    sb.Append(Z.ToString());
                    break;
                    default:
                    sb.Append(format[sourceIndex]);
                    break;
                }
                sourceIndex++;
            }
            return sb.ToString();
        }

    }


    class Program
    {
        static void Main(string[] args)
        {
            Vertex3d v = new Vertex3d(1.0, 2.0, 3.0);
            Console.WriteLine(v.ToString());

            Console.WriteLine(v.ToString("G", CultureInfo.CurrentCulture));


            Console.WriteLine(CultureInfo.CurrentCulture.TextInfo.CultureName.ToString());

            Console.ReadKey();
        }
    }
}
